package com.example.matchandroiddevices.scanlable;

import android.content.Context;
import android.util.Log;

public class HFLib {
	private static final String TAG = null;
	private byte addr=(byte)255;
	//读写器信息
	private	byte TVersionInfo[]={-1,-1};
	private	byte ReaderType[]={-1};
	private	byte TrType[]={-1};
	private	byte ScanTime[]={-1}; 
	//标签信息
	private byte state[]=new byte[1];
	private int Cardnum[]=new int[1];
	private byte pOUcharUIDList[]=new byte[8000];
	private byte Data_15693[]=new byte[256];
	
	private byte Transparent_len[]=new byte[1];
	private byte Transparent_data[]=new byte[256];
	private byte Errorcode[]=new byte[1];
	//串口属性	
	private int uhf_speed;
	private byte uhf_addr;
	private Context mContext;
	private String Serial;
	private int logswitch;
	//new 构造函数
	public HFLib(int tty_speed,String serial, int log_swith,Context mCt)
	{	
		//ModulePowerOn();
		uhf_speed = tty_speed;
		uhf_addr = addr;
		mContext = mCt;
		Serial = serial;
		logswitch = log_swith;
	}
	//打开串口，并获取读写器信息
	public int open_reader()
	{
		try
		{
			int reply1=1;
			reply1=ConnectReader(Serial,uhf_speed,logswitch);
			return reply1;
		}
		catch(Exception e)
		{
			return -1;
		}
	 }
	//获取读写器信息
	public int ReGetInfo()
	{
		try
		{
			byte RFU[]=new byte[2];
			int result=GetReaderInfo(TVersionInfo, RFU,ReaderType, TrType, ScanTime);
			if  (result==0)
			return 0;
			return -1;
		}
		catch(Exception e)
		{
			return -1;
		}
	}
	//关闭关口
   public int close_reader()
   {
	   DisconnectReader();
	   return 0;
   }
   //取得版本号
   public byte[] Get_TVersionInfo()
   {
	   return TVersionInfo;
   }
   //取得读写器类型
   public byte[] Get_ReaderType()
   {
	   return ReaderType;
   }
   //取得读写器支持协议
   public  byte[] Get_TrType()
   {
	   return TrType;
   }
   //取得读写器最大询查时间
   public byte[] Get_ScanTime()
   {
	   return ScanTime;
   } 
   //关闭射频
   public int Close_Rf()
   {
	   return CloseRf(); 
   }
   //打开射频
   public int Open_Rf()
   {
	   return OpenRf();
   }
  
   private void ClearArray(byte[] source)
   {
	   for(int i=0;i<source.length;i++) {
		   source[i]=0;
	   }
   }
   //询查命令
   public int Inventory(byte state)
   {
	   try
	   {
		   Cardnum[0]=0;
		   ClearArray(pOUcharUIDList);
		   int[] num1 = new int[1];
		   num1[0]=0;
		   byte[] uidlist= new byte[8000];
		   int result = Inventory(state,uidlist,num1);
		   Log.d("张数",String.valueOf(num1[0]));
		   if(num1[0]>0)
		   {
			   pOUcharUIDList = uidlist.clone();
			   Cardnum[0] = num1[0];
		   }
		   return result;
	   }
	   catch(Exception e)
	   {
		   return -1;
	   }
   }
   //询查到的标签列表
   public byte[] Inventory_15693()
   {
	   return pOUcharUIDList;
   } 
   //询查到的标签张数
   public int Inventory_CardNum()
   {
	   return Cardnum[0];
   } 
   //读单块
   public int ReadSingleBlock(byte state,byte UID[],byte blockNumber)
   {
	   try
	   {
		   byte SecurityWord[]=new byte[100];
		   ClearArray(Data_15693);
		   int result=ReadSingleBlock(state,UID,blockNumber,SecurityWord,Data_15693, Errorcode);
		   if(result==0)
			   return 0;
		   else
			   return -1;
	   }
	   catch(Exception e)
	   {
		   return -1;
	   }
   }
   //读取到的块数据
   public byte[] Readdata_15693()
   {
	   return Data_15693;
   } 
   //写单块数据
   public int WriteSingleBlock(byte state,byte UID[],byte blockNumber,byte Data[])
   {
	  try
	  {
		  int result=WriteSingleBlock(state,UID,blockNumber,Data, Errorcode);
		   if(result==0)
		   return 0;
		   else
		   return -1;
	  }
	  catch(Exception e)
	  {
		  return -1;
	  }
   }
   //读多块数据
   public int ReadMultipleBlock(byte state,byte UID[],byte startblockNumber,byte numberOfBlock)
   {
	   try
	   {
		   byte SecurityWord[]=new byte[100];
		   ClearArray(Data_15693);
		   int result=ReadMultipleBlock(state,UID,startblockNumber,numberOfBlock,SecurityWord,Data_15693, Errorcode);
		   if(result==0) return 0;
		   else return -1;
	   }
	   catch(Exception e)
	   {
		   return -1;
	   }
	   
   }
   //锁块
   public int LockBlock(byte state,byte UID[],byte blockNumber)
   {
	   try
	   {
		   int result=LockBlock(state,UID,blockNumber,Errorcode);
		   if(result==0) return 0;
		   else return -1;
	   }
	   catch(Exception e)
	   {
		   return -1;
	   }
	  
   }
  
   //透明命令返回长度
   public byte Tran_Len()
   {
	   return Transparent_len[0];
   }
   //透明命令返回数据
   public byte[] Tran_Data()
   {
	   return Transparent_data;
   }
	

	static{
	 	System.loadLibrary("tzf");
	}
  
	static native int ConnectReader(String serial,int speed,int log_swith);
	static native int DisconnectReader();
	static native int OpenRf();
	static native int CloseRf();
	static native int GetReaderInfo(byte VersionInfo[],
			byte RFU[],
			byte ReaderType[],
			byte TrType[],
			byte InventoryScanTime[]);
	static native int Inventory(byte state,
			byte Data[],
			int cardnum[]);
	static native int ReadSingleBlock(byte state,
			byte UID[],
			byte blockNumber,
			byte SecurityWord[],
			byte Data[],
			byte ErrorCode[]);
	static native int WriteSingleBlock(byte state,
			byte UID[],
			byte blockNumber,
			byte Data[],
			byte ErrorCode[]);
	static native int ReadMultipleBlock(byte tate,
			byte UID[],
	        byte startblockNumber,
	        byte numberOfBlock,
	        byte SecurityWord[],
	        byte Data[],
	        byte ErrorCode[]);
	static native int LockBlock(byte state,
			byte UID[],
			byte blockNumber,
			byte ErrorCode[]);
	static native int SetActiveANT(byte Antenna);

}

